/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume
 * caller calls free().
 */
 //最小绝对差
 /*给你个整数数组 arr，其中每个元素都 不相同。

请你找到所有具有最小绝对差的元素对，并且按升序的顺序返回。

每对元素对 [a,b] 如下：

a , b 均为数组 arr 中的元素
a < b
b - a 等于 arr 中任意两个元素的最小绝对差
2 <= arr.length <= 10^5
-10^6 <= arr[i] <= 10^6
*/
int compar(const void* q1, const void* q2) {
    return (*((int*)(q1)) - *((int*)(q2)));
}
int** minimumAbsDifference(int* arr, int arrSize, int* returnSize,
                           int** returnColumnSizes) {
    int min = 1000001;
    qsort(arr, arrSize, sizeof(int), compar);
    int** nums = (int**)calloc(arrSize - 1, sizeof(int*));
    for (int i = 0; i < arrSize - 1; i++) {
        nums[i] = (int*)calloc(2, sizeof(int));
    }
    int j = 0;
    for (int i = 1; i < arrSize; i++) {
        while (i < arrSize && arr[i] == arr[i - 1]) {
            i++;
        }
        if (min > (arr[i] - arr[i - 1])) {
            j = 0;
            min = arr[i] - arr[i - 1];
            nums[j][0] = arr[i - 1];
            nums[j][1] = arr[i];
            j++;
        } else if (min == arr[i] - arr[i - 1]) {
            nums[j][0] = arr[i - 1];
            nums[j][1] = arr[i];
            j++;
        }
    }

    *returnSize = j;
    *returnColumnSizes = (int *)malloc(sizeof(int) * j);
    for (int i = 0; i < j; i++) {
        (*returnColumnSizes)[i] = 2;
    }
    return nums;
}